﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class ScenicspotController : Controller
    {
        //注入
        private IScenicspotService _scenicspotService;
        public ScenicspotController(IScenicspotService scenicspotService)
        {
            _scenicspotService = scenicspotService;
        }

        #region 查询景点列表
        /// <summary>
        /// 查询景点列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetScenicspotList(int page, int limit, string data)
        {
            ResponseResult responseResult = new ResponseResult();
            Expression<Func<Scenicspot, bool>> whereLambda = a => true;
            //如果不为空则按条件查询
            if (!string.IsNullOrEmpty(data)) { whereLambda = a => a.ScenicName.Contains(data); }
            Expression<Func<Scenicspot, int>> orderbyLambda = a => a.Id;
            int total;
            List<Scenicspot>scenicspots=_scenicspotService.QueryPageList(page, limit, whereLambda, orderbyLambda, out total, true);
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            responseResult.data = scenicspots;
            //count放在最下
            responseResult.count = total;
            return Json(responseResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加景点
        /// <summary>
        /// 添加景点
        /// </summary>
        /// <param name="scenicspot"></param>
        /// <returns></returns>
        public JsonResult AddScenicspot(Scenicspot scenicspot)
        {
            ResponseResult responseResult = new ResponseResult();
            //根据ID是否为空判断添加还是编辑
            if (scenicspot.Id > 0)
            {
                responseResult.Success = _scenicspotService.Update(scenicspot);
                responseResult.msg = responseResult.Success ? "修改成功" : "修改失败";
            }
            else
            {
                responseResult.Success = _scenicspotService.Add(scenicspot);
                responseResult.msg = responseResult.Success ? "添加成功" : "添加失败";
            }
            return Json(responseResult);
        }
        #endregion

        #region 删除景点
        /// <summary>
        /// 删除景点
        /// </summary>
        /// <param name="scenicspot"></param>
        /// <returns></returns>
        public JsonResult DelScenicspot(int Id)
        {
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _scenicspotService.DeleteById(Id);
            responseResult.msg = responseResult.Success ? "删除成功" : "删除失败";
            return Json(responseResult);
        }
        #endregion

        #region 批量删除景点
        /// <summary>
        /// 批量删除景点
        /// </summary>
        /// <param name="ListId"></param>
        /// <returns></returns>
        public JsonResult BatchDeleteById(string ListId)
        {
            ResponseResult responseResult = new ResponseResult();
            //将string字符串转化为int数组
            int[] channelCIdArr = Array.ConvertAll(ListId.Split(','), s => int.Parse(s));
            //调用批量删除的方法
            responseResult.Success = _scenicspotService.BatchDeleteById(channelCIdArr);
            responseResult.msg = responseResult.Success ? "批量删除成功" : "批量删除失败";
            return Json(responseResult);
        }
        #endregion
    }
}